package cn.ljy.uem.service.impl;

import cn.ljy.authority.config.security.UserUtil;
import cn.ljy.authority.model.SysUser;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.uem.dao.SchoolCourseMapper;
import cn.ljy.uem.dao.SchoolCourseSectionMapper;
import cn.ljy.uem.dao.SchoolCourseSectionResourceMapper;
import cn.ljy.uem.dao.SchoolTeacherMapper;
import cn.ljy.uem.model.SchoolCourseSection;
import cn.ljy.uem.model.SchoolTeacher;
import cn.ljy.uem.model.query.SchoolCourseSectionQuery;
import cn.ljy.uem.service.SchoolCourseSectionService;
import cn.ljy.uem.service.common.CommonSchoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;


/**
 * ischool课程章节实现
 *
 * @author 小锋
 * @date 2022/08/22
 */
@Service
@Slf4j
public class ISchoolCourseSectionService extends IBaseService<SchoolCourseSection, SchoolCourseSectionQuery> implements SchoolCourseSectionService {
    @Autowired
    private SchoolCourseSectionMapper mapper;
    @Autowired
    private SchoolCourseMapper schoolCourseMapper;
    @Autowired
    private SchoolTeacherMapper schoolTeacherMapper;

    @Autowired
    private SchoolCourseSectionResourceMapper schoolCourseSectionResourceMapper;

    @Autowired
    private CommonSchoolService commonSchoolService;

    @Override
    protected void beforeGet(Serializable id) {

    }

    //验证
    private void beforeSaveOrUpdate(SchoolCourseSection schoolCourseSection){
        //验证当前登录账户是否有该课程的权限
        commonSchoolService.validateCourseManageAuthorityOfLoginTeacher(schoolCourseSection.getCourseId());
        //判断课程是否存在
        if(schoolCourseMapper.findById(schoolCourseSection.getCourseId())==null){
            throw new MyWebException("操作失败：课程不存在，请选择其他课程！");
        }
    }

    @Override
    protected void beforeUpdate(SchoolCourseSection schoolCourseSection) {
        beforeSaveOrUpdate(schoolCourseSection);
    }

    @Override
    protected void beforeSave(SchoolCourseSection schoolCourseSection) {
        beforeSaveOrUpdate(schoolCourseSection);
        //获取当前登录信息
        SysUser sysUser = UserUtil.getLoginUser();
        if(sysUser!=null) {//如果当前登录账户存在
            //获取当前登录账户对应的教师信息
            SchoolTeacher creator = schoolTeacherMapper.getTeacherByUserId(sysUser.getId());
            if(creator!=null){
                schoolCourseSection.setCreator(creator);
            }
            if(schoolCourseSection.getRecycle()==null){
                schoolCourseSection.setRecycle(false);
            }
        }

    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
        for(Serializable id:ids){
            //获取原来的章节id
            SchoolCourseSection schoolCourseSection=mapper.findById(id);
            //验证当前登录账户是否有该课程的权限
            commonSchoolService.validateCourseManageAuthorityOfLoginTeacher(schoolCourseSection.getCourseId());
            SchoolCourseSectionQuery FindSchoolCourseSection=new SchoolCourseSectionQuery();
            FindSchoolCourseSection.setParentId(schoolCourseSection.getId());
            //判断如果存在父章节 不删除
            if (mapper.findAll(FindSchoolCourseSection).size()>0){
                throw new MyWebException("操作失败：章节存在父章节无法删除");
            }


        }
        //移除章节对应的资源
        schoolCourseSectionResourceMapper.deleteBySectionIds(ids);
    }
}
